﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Reticulum.MasterForm
{
    public partial class jurisdiction : Form
    {
        public jurisdiction()
        {
            InitializeComponent();
        }
        DataSet ds = new DataSet();
        private void Jurisdiction_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DBHelps.Adapter("select UserName,UploadState,DownloadState from Users").Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)//查询
        {
            string UserName = textBox1.Text;//获取输入的字符

            DataSet ds = new DataSet();
            var sql = string.Format("select UserName,UploadState,DownloadState from Users where UserName='{0}'",textBox1.Text);
            SqlDataReader reader = DBHelps.Select(sql);
            if (reader.Read())
            {
                username.Text = reader["UserName"].ToString();

                if (reader["UploadState"].ToString() == "0")
                {
                    upload_text.Text = "允许上传";
                }
                else
                {
                    upload_text.Text = "禁止上传";
                }

                if (reader["DownloadState"].ToString() == "0")
                {
                    download_text.Text = "允许下载";
                }
                else
                {
                    download_text.Text = "禁止下载";
                }
            }
            else
            {
                MessageBox.Show("未查到该用户！");
            }
            reader.Close();
            DBHelps.reader = null;
        }

        private void Upload_changes_Click(object sender, EventArgs e)//上传权限
        {
            if (upload_text.Text == "允许上传")
            {
                if (DBHelps.Command(string.Format("update Users set UploadState=1 where UserName='{0}'", textBox1.Text)))
                {
                    MessageBox.Show("修改成功！");
                    button1_Click(sender, e);
                    Jurisdiction_Load(sender, e);
                }
            }
            else
            {
                if (DBHelps.Command(string.Format("update Users set UploadState=0 where UserName='{0}'", textBox1.Text)))
                {
                    MessageBox.Show("修改成功！");
                    button1_Click(sender, e);
                    Jurisdiction_Load(sender, e);
                }
            }
        }

        private void Download_changes_Click(object sender, EventArgs e)//下载权限
        {
            if (download_text.Text == "允许下载")
            {
                if (DBHelps.Command(string.Format("update Users set DownloadState=1 where UserName='{0}'", textBox1.Text)))
                {
                    MessageBox.Show("修改成功！");
                    button1_Click(sender, e);
                    Jurisdiction_Load(sender, e);
                }
            }
            else
            {
                if (DBHelps.Command(string.Format("update Users set DownloadState=0 where UserName='{0}'", textBox1.Text)))
                {
                    MessageBox.Show("修改成功！");
                    button1_Click(sender, e);
                    Jurisdiction_Load(sender, e);
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)//键入回车转到查询按钮
            {
                button1_Click(sender, e);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = dataGridView1.Columns.Count;

        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Jurisdiction_Load(sender, e);
        }
    }
    }
